package com.honeycomb.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.honeycomb.common.base.dao.impl.BaseDaoImpl;
import com.honeycomb.db.dao.TaginfoDao;
import com.honeycomb.tag.dto.TagInfoDto;

/**
 * 文章dao实现
 * 
 * @author Wangyu
 */
@Repository
public class TaginfoDaoImpl extends BaseDaoImpl implements TaginfoDao {
	
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	/*
	 * @author zhangchengwei 
	 * 根据用户Id查找用户关注的标签
	 * */
	@Override
	public List<TagInfoDto> getTagsByUser(String userId) {
		logger.info("---TaginfoDaoImpl---getTagsByUser---");
		
		String sql = "select * from tag_info t, tag_user_rel tu where t.tag_id = tu.tag_id and tu.user_id=?;";
		Object[]  val = {userId};
		List<Map<String,Object>> result = jdbcTemplate.queryForList(sql, val);
		
		List<TagInfoDto> tagList = new ArrayList<TagInfoDto>(result.size());
		TagInfoDto tagInfoDto = null;
		for (Map<String,Object> elem : result){
			tagInfoDto = new TagInfoDto((String) elem.get("tag_id"));
			tagInfoDto.setParentId((String) elem.get("parent_id"));
			tagInfoDto.setTagName((String) elem.get("tag_name"));
			tagInfoDto.setTagLogoUrl((String) elem.get("tag_logo"));
			
			tagList.add(tagInfoDto);
		}
		
		return tagList;
	}

	
	/**
	 * 给定一个标签，查询出所有下级标签
	 */
	@Override
	public List<TagInfoDto> getChildrenTags(String tagId) {

		logger.info("-------TaginfoDaoImpl---selectChildrenTags-----------------");
		
		String sql = "select * from tag_info where parent_id=?;";
		Object[]  val = {tagId};
		List<Map<String,Object>> result = jdbcTemplate.queryForList(sql, val);
		
		List<TagInfoDto> tagList = new ArrayList<TagInfoDto>(result.size());
		TagInfoDto tagInfoDto = null;
		for (Map<String,Object> elem : result){
			tagInfoDto = new TagInfoDto((String) elem.get("tag_id"));
			tagInfoDto.setParentId((String) elem.get("parent_id"));
			tagInfoDto.setTagName((String) elem.get("tag_name"));
			tagInfoDto.setTagLogoUrl((String) elem.get("tag_logo"));
			
			tagList.add(tagInfoDto);
		}
		
		return tagList;
	}

	/**
	 * 查询所有一级标签
	 */
	@Override
	public List<TagInfoDto> getRootTags() {
		logger.info("-------TaginfoDaoImpl---getRootTags-----------------");
		
		String sql = "select * from tag_info where parent_id=?;";
		Object[]  val = {TagInfoDto.ROOT_TAG_PARENT_ID};
		List<Map<String,Object>> result = jdbcTemplate.queryForList(sql, val);
		
		List<TagInfoDto> tagList = new ArrayList<TagInfoDto>(result.size());
		TagInfoDto tagInfoDto = null;
		for (Map<String,Object> elem : result){
			tagInfoDto = new TagInfoDto((String) elem.get("tag_id"));
			tagInfoDto.setParentId((String) elem.get("parent_id"));
			tagInfoDto.setTagName((String) elem.get("tag_name"));
			tagInfoDto.setTagLogoUrl((String) elem.get("tag_logo"));
			
			tagList.add(tagInfoDto);
		}
		
		return tagList;
	}

	@Override
	public boolean attentionTag(String userId, String tagId) {
		// TODO Auto-generated method stub
		boolean flag = false;
		//将userId和tagId关联到tag_user_rel中
		String sql = "insert into tag_user_rel values(?, ?);";
		jdbcTemplate.update(sql, new Object[]{userId, tagId});
		flag = true;
		return flag;
	}

	@Override
	public boolean deleteFavorTag(String tagId) {
		// TODO Auto-generated method stub
		boolean flag = false;
		String sql = "delete from tag_user_rel where tag_id=?;";
		jdbcTemplate.update(sql, new Object[]{tagId});
		flag = true;
		return flag;
	}
	
}
